<?php
	require("../include/dbfunctions.php");
session_start();
	if(isset($_GET['out']) || (isset($_SESSION['time']) && $_SESSION['time']+600<time())){
		session_destroy();
		header("Location: {$_SERVER['PHP_SELF']}");
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta content="text/html; charset=UNICODE-1" http-equiv="Content-Type" />
	<title>PolibaMarks</title>
	<link rel="stylesheet" href="stile.css"/>
</head>

<body>
	<div id="imgLogo">
		<img id="imgLogo" src="img/logopoliba.jpg" alt="Logo" />
	</div>

<?php
if(isset($_POST['u']) && isset($_POST['p'])){
	$psw = md5($_POST['p']);
	$db = new Session();
	if($db->userExists($_POST['u'], $psw)){
			$_SESSION['user'] = $_POST['u'];
			$_SESSION['psw'] = $psw;
			$_SESSION['idpsw'] = md5($_POST['u'].$_POST['p']);
			$_SESSION['time'] = time();
	}
	else 
		echo '<p id="loginError">Nome utente o password errati.</p>';

}
if(isset($_SESSION['user']) && isset($_SESSION['psw']) && isset($_SESSION['time']))
		if($_SESSION['time']+600 > time()){
			$_SESSION['time'] = time();
			require("../include/home.php");
		}
		else {
			session_destroy();
			echo '<p id="sessionExpired">Sessione scaduta. <button onclick="javascript:history.back();"> Indietro </button></p>';
		}
			
else {

	if(isset($_GET['signup']))
	{
		$sign = new Session();
		if(isset($_POST['nome'], $_POST['cognome'], $_POST['mail'], $_POST['pass'], $_POST['cdl'], $_POST['anno']))
			if($sign->register($_POST['nome'], $_POST['cognome'], $_POST['mail'], $_POST['pass'], $_POST['cdl'], $_POST['anno']))
				echo "Registrato con successo!";
			else 
			echo "Registrazione fallita.";
?>
	<div id="login">
		<h2>Registrati subito!</h2><br />
		
		<form class="sign" method="post">
		<table border="0">
			<tr><td>Nome:</td><td><input size="13" type="text" name="nome" /></td></tr>
			<tr><td>Cognome:</td><td> <input size="13" type="text" name="cognome" /> </td></tr>
			<tr><td>Email:</td><td> <input size="13" type="text" name="mail" /> </td></tr>
			<tr><td>Password:</td><td> <input size="13" type="password" name="pass" value=""/></td></tr>
			<tr><td>CdL:</td><td> <select  name="cdl">
<?php
			$cdl = $sign->getCdLs();
			foreach($cdl as $c)
				echo "<option value='{$c[0]}'>{$c[1]}</option>";
?>
					
				  </select></td></tr>
			<tr><td>Anno:</td><td> <select name="anno">
					<option value="1" selected="selected">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
				</select></td></tr>
			<tr><td></td><td><input type="submit" value="Iscriviti!"/></td></tr>
		</table>
		</form>
		<a href="javascript:history.back()">Indietro</a>
	</div>
<?php	
	}
	else 
	{
?>		<div id="login">
			<form method="post">
					User: <input size="13" type="text" name="u" maxlength="120" />
				<br/>Pass: <input size="13" type="password" name="p" maxlength="8" />
				<br/><input type="submit" value="Login" />
			</form>
			<a href="lostpsw.php">Recupero password</a>
			<br/>
			<a href="?signup=A7BC">Registrati</a>
			<br/>
			<br/>
			<a href="professor/">Area docenti</a>
		</div>
<?php	}
 }
?>
</body>

</html>